home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
BARNET
/
COMPILER
/
SATHER
/
!Sather
/
Library
/
Containrs
/
sa
/
multi_incl
< prev
next >
Wrap
Text File
|
1996-06-01
|
5KB
|
161 lines
---------------------------> Sather 1.1 source file <--------------------------
-- multimap_incl.sa: Multimap include partial class
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: multimap_incl.sa,v 1.6 1996/06/01 21:36:30 gomes Exp $
--
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
-------------------------------------------------------------------
class MULTIMAP{K,E} is
include H_MULTIMAP{K,E};
end;
-------------------------------------------------------------------
partial class RO_MULTIMAP_INCL{K,E} is
-- Partial class for multi-maps
include COMPARE{E};
-- --------- CORE FEATURES (must define) ---------
stub size: INT;
-- The number of elements in this multimap
stub n_inds: INT;
-- Return the total number of indices
stub elt!: E;
-- Yield elements (unordered)
stub ind!: K;
-- Yield element indices (unordered)
stub pair!: TUP{K,E};
-- Yield pairs of index,element
stub target!(once k:K): E;
-- Yield the targets of the index "k"
stub n_targets(k:K): INT;
-- Return the number of targets for index "k"
-- ------ Access/Measurement --------------
targets(k: K): BAG{E} is
res ::= #BAG{E};
loop res.insert(target!(k)) end;
return res;
end;
inds: ARRAY{K} is
-- Return an index array which is the same size as self and
-- is set to the values of the indices
sz: INT := n_inds;
res: ARRAY{K} := #(sz);
i: INT := 0;
loop until!(i >= sz); res[i] := ind!; i := i + 1; end;
return res;
end;
target!: E is loop yield elt! end; end;
-- ------ Queries/Comparison --------------
has_ind(k: K): BOOL is return n_targets(k) > 0 end;
-- Return true if the index "k" has at least one target in this
-- multimap
has_elt(e: E): BOOL is return has(e) end;
has(e: E): BOOL is
-- Return true if this multimap has the element "e"
loop t ::= elt!; if elt_eq(t,e) then return true end; end;
return false;
end;
equals(b: $RO_MULTIMAP{K,E}): BOOL is
-- Returns true if all of "e"'s elements are equal to self's elts
-- Ordering is an issue. Should be redefined to be more
-- precise for particular descendants
if n_inds /= b.n_inds then return false end;
if size /= b.size then return false end;
loop e ::= ind!;
if n_targets(e) /= b.n_targets(e) then return false end;
end;
return true ;
end;
elt_if(test:ROUT{E}:BOOL,out res:E):BOOL is
-- Return the first element that satisfies "test" in "res"
-- Return true if a element was found, false otherwise
loop r ::= elt!; if test.call(r) then res := r; return true; end; end;
return false;
end;
ind_if(test:ROUT{E}:BOOL):K is
-- Return the index of the leftmost element that satisfies `test',
-- or void if there is none.
-- Must be changed to use an out argument
loop
r::=ind!;
loop e ::= target!(r);
if test.call(e) then return r end
end;
end;
return void
end;
-- ------ Cursor --------------------------
filter!(once f:ROUT{E}:BOOL): E pre ~void(self) is
-- Yield all elements that satisfy the boolean predicate "f"
loop
e ::= target!;
if f.call(e) then yield e end
end
end;
filter_not!(once f:ROUT{E}:BOOL): E pre ~void(self) is
-- Yield all elements that do not satisfy the boolean predicate "f"
loop e ::= target!; if ~f.call(e) then yield e end end
end;
-- ------ Conversion ----------------------
str: STR is
-- Prints out a string version of the array of the components
-- that are under $STR, and their associated indices
res ::= #FSTR("{");
loop
p ::= pair!;
res := res+",".separate!("["+ind_str(p.t1)+"]="+elt_str(p.t2));
end;
res := res +"}";
return(res.str);
end;
str_of_elts: STR is
-- Prints out a string version of the array of the components
-- that are under $STR, and their associated indices
res ::= #FSTR("");
loop res:=res+",".separate!(elt_str(elt!)); end;
return(res.str);
end;
-- ------ Basic Operations ----------------
private ind_str(i: K): STR is
typecase i
when $STR then return i.str else return "Unprintable Index" end;
end;
private elt_str(e: E): STR is
typecase e
when $STR then return e.str else return "Unprintable Element" end;
end;
end;
--=============================================================================
partial class MULTIMAP_INCL{K,E} is
-- Partial class for full fledged multi-maps
include RO_MULTIMAP_INCL{K,E};
end;
--=============================================================================